package org.apogee.mil.client.main.pages.admin.forms;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FileUploadField;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.FormPanel.Encoding;
import com.extjs.gxt.ui.client.widget.form.FormPanel.Method;
import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.RequestTimeoutException;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.Element;

public class FileUpload extends LayoutContainer{

	@Override  
	protected void onRender(Element parent, int index) {  
		super.onRender(parent, index);  

		final String url = GWT.getModuleBaseURL() + "fileupload";

		setStyleAttribute("margin", "2px");  

		final FormPanel panel = new FormPanel();  
		panel.setHeading("Test Upload");
		panel.setAction(url);
		panel.setFrame(true);    
		panel.setEncoding(Encoding.MULTIPART);  
		panel.setMethod(Method.POST);  
		panel.setButtonAlign(HorizontalAlignment.CENTER);  
		panel.setWidth(500);  

		final FileUploadField file = new FileUploadField() {
			@Override
			protected void onChange(ComponentEvent ce) {
				super.onChange(ce);

				String initValue = getValue();
				String newVal = initValue.substring(initValue.lastIndexOf("\\") + 1, 
						initValue.length());
				setValue(newVal);
			}
		};

		file.setAllowBlank(false);  
		file.setName("uploadedfile");  
		file.setFieldLabel("File");
		
		panel.setLabelWidth(150);

		panel.add(file);  

		Button btn = new Button("Reset");
		btn.addSelectionListener(new SelectionListener<ButtonEvent>() {  
			@Override  
			public void componentSelected(ButtonEvent ce) {  
				panel.reset();  
			}  
		});  
		panel.addButton(btn);  

		btn = new Button("Submit");  
		btn.addSelectionListener(new SelectionListener<ButtonEvent>() {  
			@Override  
			public void componentSelected(ButtonEvent ce) {  
				if (!panel.isValid()) {  
					return;  
				}  

				panel.submit();
				panel.mask("Please Wait...");
				RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);

				try {
					/*
					 * wait 10000 milliseconds for the request to complete
					 */
					builder.setTimeoutMillis(15000);

					@SuppressWarnings("unused")
					Request response = builder.sendRequest(null, new RequestCallback() {
						public void onError(Request request, Throwable exception) {
							if (exception instanceof RequestTimeoutException) {
								MessageBox.info("Success", "Questions Uploaded Succesfully", null);
								panel.unmask();
								panel.reset();
							} else {
								MessageBox.alert("Error", "Unable to upload file", null);
								panel.unmask();
								panel.reset();
							}
						}

						public void onResponseReceived(Request request, Response response) {
							panel.unmask();
							MessageBox.info("Success", response.getText(), null);
							panel.reset();
						}


					});
	
				} catch (RequestException e) {
					//Window.alert("Failed to send the request: " + e.getMessage());
				}
			}  
		});

		panel.addButton(btn);  

		add(panel);  
	}

}
